package com.ntty.server.handle;


import com.ntty.utils.HttpTools;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
/**
 * http 服务句柄实现
 * @author 37798955@qq.com
 *
 */
public class HttpServerHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpRequest fullHttpRequest) {
        System.out.println(fullHttpRequest);
        String responseContent;
        HttpResponseStatus responseStatus = HttpResponseStatus.OK;
        if (fullHttpRequest.method() == HttpMethod.GET) {
            System.out.println(HttpTools.getGetParamasFromChannel(fullHttpRequest));
            responseContent = "GET method over";
        } else if (fullHttpRequest.method() == HttpMethod.POST) {
            System.out.println(HttpTools.getPostParamsFromChannel(fullHttpRequest));
            responseContent = "POST method data";
        } else {
            responseStatus = HttpResponseStatus.INTERNAL_SERVER_ERROR;
            responseContent = "INTERNAL_SERVER_ERROR";
        }
        FullHttpResponse response = HttpTools.getTextResponse(responseStatus, responseContent);
        channelHandlerContext.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }

 

  
}
